from AStock.ASEvent import Engine, Task
from example.event.job import CrawlJob
import time


def process_sina_result(crawl_result):
    print(crawl_result)


def main():
    engine = Engine()
    engine.start()
    engine.create_event('e_crawl_sina')
    engine.start_event('e_crawl_sina')
    engine.create_event('e_crawl_tencent')
    engine.start_event('e_crawl_tencent')

    task1 = Task(
        {'event_type': 'crawl_sina'},
        CrawlJob(),
        bind_event='e_crawl_sina',
        name='crawlTask1',
        callback=process_sina_result
    )
    # 放入对应的qthread线程
    engine.run_task(task1)

    task2 = Task(
        {'event_type': 'crawl_tencent'},
        CrawlJob(),
        bind_event='e_crawl_tencent',
        name='crawlTask2',
        callback=print
    )
    # 也可以直接放入engine的队列，由engine分派执行线程
    engine.put(task2)

    time.sleep(2)
    engine.pause()
    engine.pause_event('e_crawl_tencent')
    time.sleep(2)
    engine.resume()
    engine.resume_event('e_crawl_tencent')

    engine.stop_all()
    engine.join_all()


if __name__ == '__main__':
    main()